package com.hktf.corelib.utils;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * @包名称： com.yx.corelib.utils
 * @类名： ByteUtils
 * @创建人： 陈大专
 * @时间： 2021/03/23 15:32
 * @描述：
 */
public class ByteUtils {

    /**
     *
     * @param n 要转化为16进制的数值
     * @param len 转化后的长度
     * @return
     */
    public static String intToHexNOox(int n,int len) {
        try {
            StringBuilder builder=new StringBuilder();
            Integer in=Integer.valueOf(n);
            String s = Integer.toHexString(in).toUpperCase();
            int length = s.length();

            int i = len - length;
            for (int i1 = 0; i1 < i; i1++) {
                builder.append("0");
            }
            builder.append(s);
            return builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     *
     * @param n 要转化为16进制的数值
     * @param len 转化后的长度
     * @return
     */
    public static String intToHex(int n,int len) {
        try {
            StringBuilder builder=new StringBuilder();
            builder.append("0x");
            Integer in=Integer.valueOf(n);
            String s = Integer.toHexString(in).toUpperCase();
            int length = s.length();

            int i = len - length;
            for (int i1 = 0; i1 < i; i1++) {
                builder.append("0");
            }
            builder.append(s);
            return builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 将int转为高字节在前，低字节在后的byte数组（大端）
     *
     * @param n int
     * @return byte[]
     */
    public static byte[] intToByteBig(int n) {
        byte[] b = new byte[4];
        b[3] = (byte) (n & 0xff);
        b[2] = (byte) (n >> 8 & 0xff);
        b[1] = (byte) (n >> 16 & 0xff);
        b[0] = (byte) (n >> 24 & 0xff);
        return b;
    }

    public static final BigDecimal readUnsignedLong(long value) throws IOException {
        if (value >= 0)
            return new BigDecimal(value);
        long lowValue = value & Long.MAX_VALUE;
        return BigDecimal.valueOf(lowValue).add(BigDecimal.valueOf(Long.MAX_VALUE)).add(BigDecimal.valueOf(1));
    }

    public static byte[] short2byte(short s){
        byte[] b = new byte[2];
        for(int i = 0; i < 2; i++){
            int offset = 16 - (i+1)*8; //因为byte占4个字节，所以要计算偏移量
            b[i] = (byte)((s >> offset)&0xff); //把16位分为2个8位进行分别存储
        }
        return b;
    }
    /**
     * 把byte转为字符串的bit
     */
    public static String byteToBit(byte b) {
        return ""
                + (byte) ((b >> 7) & 0x1) + "," + (byte) ((b >> 6) & 0x1) + ","
                + (byte) ((b >> 5) & 0x1) + "," + (byte) ((b >> 4) & 0x1) + ","
                + (byte) ((b >> 3) & 0x1) + "," + (byte) ((b >> 2) & 0x1) + ","
                + (byte) ((b >> 1) & 0x1) + "," + (byte) ((b >> 0) & 0x1);
    }

    public static short byte2short(Byte[] b) {
        short l = 0;
        for (int i = 0; i < 2; i++) {
            l <<= 8; //<<=和我们的 +=是一样的，意思就是 l = l << 8
            l |= (b[i] & 0xff); //和上面也是一样的  l = l | (b[i]&0xff)
        }
        return l;
    }
    public static short byte2short(byte[] b) {
        short l = 0;
        for (int i = 0; i < 2; i++) {
            l <<= 8; //<<=和我们的 +=是一样的，意思就是 l = l << 8
            l |= (b[i] & 0xff); //和上面也是一样的  l = l | (b[i]&0xff)
        }
        return l;
    }

    /**
     * byte 转16进制string
     *
     * @param b
     * @return
     */
    public static String byte2Hex(byte b) {
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        return hex;
    }

    /**
     * 将字节数组以十六进行打印
     *
     * @param b
     */
    public static void printHexString(byte[] b) {
        String result = "16进制byte:\n";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            result += "   " + hex.toUpperCase();
            if ((i+1) % 32 == 0) {
                result += "\n";
            }
        }
        LogMds.w("cdz", result);
    }
    /**
     * 将字节数组以十六进行打印
     *
     * @param b
     */
    public static void printHexString(String msg, byte[] b) {
        String result = msg + ":";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            result += "   " + hex.toUpperCase();
        }
        LogMds.w("cdz", result);
    }

    /**
     * Hex转byte,hex只能含两个字符，如果是一个字符byte高位会是0
     */
    public static byte hexTobyte(String hex) {
        return (byte) Integer.parseInt(hex, 16);
    }
}
